package com.cskaoyan.service.mall;

import com.cskaoyan.bean.po.ohter.MarketIssue;
import com.cskaoyan.bean.po.ohter.MarketIssueExample;
import com.cskaoyan.bean.vo.mall.IssueListInfo;
import com.cskaoyan.bean.vo.mall.IssueListVo;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Ajun de BBC
 * @Date 2022/7/16 18:54
 * @Version 1.5
 */

@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;

    /**
     *@Description :通用问题的界面显示
     *@Param :
     *@Return :
     */
    @Override
    public IssueListVo issueList(Integer page, Integer limit,String question, String sort, String order) {
        PageHelper.startPage(page,limit);//开启分页
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        //order by add_time desc
        marketIssueExample.setOrderByClause(sort + " " + order);
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();//动态拼接sql语句
        question = (question == null) ? "%" : "%" + question + "%";
        criteria.andQuestionLike(question);
        criteria.andDeletedEqualTo(false);
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(marketIssueExample);

        List<IssueListInfo> issueListInfos = new ArrayList<>();
        for (MarketIssue marketIssue : marketIssues) {
            IssueListInfo issueListInfo = new IssueListInfo();
            BeanUtil.copyBean(marketIssue,issueListInfo);
            issueListInfos.add(issueListInfo);
        }

        PageInfo<MarketIssue> objectPageInfo = new PageInfo<>(marketIssues);
        Long total = objectPageInfo.getTotal();
        int pages = objectPageInfo.getPages();

        return new IssueListVo(total.intValue(),pages,limit,page,issueListInfos);
    }

    /**
     *@Description : 通用问题的增加
     *@Param :
     *@Return :
     */
    @Override
    public IssueListInfo issueCreate(IssueListInfo info) {
        info.setAddTime(new Date(System.currentTimeMillis()));
        info.setUpdateTime(new Date(System.currentTimeMillis()));
        info.setDeleted(false);
        marketIssueMapper.insertOneIssue(info);
        return info;
    }

    /**
     *@Description : 通用问题的修改
     *@Param :
     *@Return :
     */
    @Override
    public void issueUpdate(IssueListInfo info) {
        info.setUpdateTime(new Date(System.currentTimeMillis()));
        MarketIssue marketIssue = new MarketIssue();
        BeanUtil.copyBean(info,marketIssue);
        marketIssueMapper.updateByPrimaryKey(marketIssue);
    }

    /**
     *@Description : 通用问题的删除（假性删除） TRUE（1）表示删除不显示在界面上  FALSE(0)表示正常显示
     *@Param :
     *@Return :
     */
    @Override
    public void issueDelete(IssueListInfo info) {
        info.setDeleted(true);
        MarketIssue marketIssue = new MarketIssue();
        BeanUtil.copyBean(info,marketIssue);
        marketIssueMapper.updateByPrimaryKey(marketIssue);
    }




}
